package com.idopartx.phonelightning.ui.permissionguide

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.dotools.umlibrary.UMPostUtils
import com.idopartx.phonelightning.MainActivity
import com.idopartx.phonelightning.R
import com.idopartx.phonelightning.common.Constant
import com.idopartx.phonelightning.databinding.ActivityGeneralSettingBinding
import com.idopartx.phonelightning.utils.PermissionUtils
import com.idopartx.phonelightning.utils.PreferenceSetting
import com.tencent.mmkv.MMKV

/****小米 OPPO 7,8,9***/
class HelpVivoPermissionActivity : AppCompatActivity() {
    var isJumpAutoStart = false
    var isJumpBackgroundRun = false
    var isHaveFloatView = false
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        DataBindingUtil.setContentView<ActivityGeneralSettingBinding>(
            this,
            R.layout.activity_vivo_setting
        )

        /**悬浮窗、自启动**/
        findViewById<Button>(R.id.start_float_btn).setOnClickListener {
            if (PermissionUtils.isVivoDevice()) {
                UMPostUtils.onEvent(applicationContext, "authority_vivo_float_auto_go_on_click")
                VivoFloatViewGuideDialog(this, object : VivoFloatViewGuideDialog.DialogCallBack {
                    override fun onOk() {
                        isJumpAutoStart = true
                        UMPostUtils.onEvent(
                            applicationContext,
                            "authority_vivo_allow_jump"
                        )
                        PermissionUtils.startFloatViewPermission(this@HelpVivoPermissionActivity)
                        updatePermissionStatus()
                    }
                }).show()
            } else {
                UMPostUtils.onEvent(this, "authority_auto_click")
                FindAndOpenDialog(this, object : FindAndOpenDialog.DialogCallBack {
                    override fun onOk() {
                        isJumpAutoStart = true
                        UMPostUtils.onEvent(
                            applicationContext,
                            "authority_auto_jump"
                        )
                        PermissionUtils.startFloatViewPermission(this@HelpVivoPermissionActivity)
                        updatePermissionStatus()
                    }
                }).show()
            }
        }

        /**后台运行**/
        findViewById<Button>(R.id.start_background_btn).setOnClickListener {

            if (PermissionUtils.isVivoDevice()) {
                UMPostUtils.onEvent(applicationContext, "authority_vivo_allow_click")
                VivoBackgroundRunGuideDialog(
                    this,
                    object : VivoBackgroundRunGuideDialog.DialogCallBack {
                        override fun onOk(index: Int) {
                            UMPostUtils.onEvent(
                                applicationContext,
                                "authority_vivo_allow_jump"
                            )
                            PermissionUtils.startVivoPowerSetting(this@HelpVivoPermissionActivity)
                            isJumpBackgroundRun = true
                            updatePermissionStatus()
                        }
                    }).show()
            } else {
                UMPostUtils.onEvent(applicationContext, "authority_allow_click");
                HuaweiAutoStartGuideDialog(
                    this,
                    object : HuaweiAutoStartGuideDialog.DialogCallBack {
                        override fun onOk(index: Int) {
                            UMPostUtils.onEvent(
                                applicationContext,
                                "authority_allow_jump"
                            )
                            PermissionUtils.startAutoStartSetting(this@HelpVivoPermissionActivity)
                            isJumpBackgroundRun = true
                            updatePermissionStatus()
                        }
                    }).show()
            }
        }

        findViewById<Button>(R.id.next_step).setOnClickListener {
//            MMKV.defaultMMKV().encode(Constant.MMKV_IS_GUIDE, true)
            PreferenceSetting.setFirst(this, false)
            UMPostUtils.onEvent(this, "authority_finish_click")
            val intent = Intent(this, MainActivity::class.java)
            intent.putExtra("isSplash", true)
            startActivity(intent)
            finish()
        }

    }

    private fun updatePermissionStatus() {
        if (isJumpAutoStart) {
            findViewById<Button>(R.id.start_float_btn).setBackgroundResource(R.drawable.shape_solid_42d174_radius_4)
            findViewById<Button>(R.id.start_float_btn).text = "已开启"
        } else {
            findViewById<Button>(R.id.start_float_btn).setBackgroundResource(R.drawable.shape_solid_3ac8bf_radius_4)
        }

        if (isJumpBackgroundRun) {
            findViewById<Button>(R.id.start_background_btn).setBackgroundResource(R.drawable.shape_solid_42d174_radius_4)
            findViewById<Button>(R.id.start_background_btn).text = "已开启"
        } else {
            findViewById<Button>(R.id.start_background_btn).setBackgroundResource(R.drawable.shape_solid_3ac8bf_radius_4)
        }
    }

    override fun onResume() {
        super.onResume()
        updatePermissionStatus()
    }
}